package com.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mall.entity.Orderproduct;
import com.mall.entity.Orderreturn;
import com.mall.entity.Product;
import com.mall.mapper.OrderproductMapper;
import com.mall.mapper.OrdersMapper;
import com.mall.mapper.ProductMapper;
import com.mall.mapper.UserMapper;
import com.mall.service.OrderproductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author fire
 * @since 2020-04-16
 */
@Service
public class OrderproductServiceImpl extends ServiceImpl<OrderproductMapper, Orderproduct> implements OrderproductService {

    @Autowired
    private OrderproductMapper orderproductMapper;
    
    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private UserMapper userMapper;
    
    @Autowired
    private ProductMapper productMapper;
    
    
    @Override
    public int addOrderproduct(int orderId, List<Product> products) {
        
        Orderproduct orderproduct = new Orderproduct();
        
        for (Product product : products) {
            orderproduct.setCount(product.getCount());
            orderproduct.setOrderId(orderId);
            orderproduct.setProductId(product.getId());
            orderproductMapper.insert(orderproduct);
        }
      
        return 1;
    }


    @Override
    public List<Orderproduct> getEvaluates(int orderId, int type) {
        QueryWrapper<Orderproduct> wrapper = new QueryWrapper<>();
        if (orderId > 0) {
            wrapper.eq("order_id", orderId);
        }
        if (type < 1) {
            wrapper.orderByDesc("start");
        } else {
            wrapper.orderByAsc("start");
        }
       
        List<Orderproduct> list = orderproductMapper.selectList(wrapper);
        for (Orderproduct o : list) {
            o.setProduct(productMapper.selectById(o.getProductId()));
            o.setUser(userMapper.selectById(ordersMapper.selectById(o.getOrderId()).getUserId()));
        }
        return list;
    }
}
